#ifndef ____SMOOTHER_h___
#define ____SMOOTHER_h___

#include <vector>

namespace MyCommon
{

	/**	Template class to help calculate the average value of a history
		of values. This can only be used with types that have a 'zero'
		value and that have the += and / operators overloaded.
		Example: Used to smooth frame rate calculations.
	*/
	template <class T>
	class Smoother
	{
	private:
		//this holds the history
		std::vector<T>  mHistory;

		int mNextUpdateSlot;

		//an example of the 'zero' value of the type to be smoothed. This
		//would be something like Vector2(0,0)
		T mZeroValue;

	public:

		//to instantiate a Smoother pass it the number of samples you want
		//to use in the smoothing, and an example of a 'zero' type
		Smoother(int SampleSize, T ZeroValue)
			:mHistory(SampleSize, ZeroValue),
			mZeroValue(ZeroValue),
			mNextUpdateSlot(0){}

		//each time you want to get a new average, feed it the most recent value
		//and this method will return an average over the last SampleSize updates
		T update(const T& MostRecentValue)
		{  
			//overwrite the oldest value with the newest
			mHistory[mNextUpdateSlot++] = MostRecentValue;

			//make sure m_iNextUpdateSlot wraps around. 
			if (mNextUpdateSlot == mHistory.size()) mNextUpdateSlot = 0;

			//now to calculate the average of the history list
			T sum = mZeroValue;

			std::vector<T>::iterator it = mHistory.begin();

			for (it; it != mHistory.end(); ++it)
			{
				sum += *it;
			}

			return sum / (double)mHistory.size();
		}

	};
}


#endif